package com.wq.reference;

/**
 * @Author: wangqiang20995
 * @Date:2018/10/18
 * @Description:
 * @Resource:
 */
public class ReferenceDemo {

    public static void main(String args[]){
        IntType obj1 = new IntType(1);
        IntType obj2 = new IntType(2);

        System.out.println("swap1");
        swap1(obj1,obj2);
        show(new IntType[]{obj1,obj2});

        System.out.println("swap2");
        swap2(obj1,obj2);
        show(new IntType[]{obj1,obj2});

    }

    //直接改变内部引用，所以会变
    private static void swap1(IntType intType1,IntType intType2){
        int value1 = intType1.getValue();
        intType1.setValue(intType2.getValue());
        intType2.setValue(value1);
    }

    //形参改变，不影响结果，所以不会改变
    private static void swap2(IntType intType1,IntType intType2){
        IntType swap = intType1;
        intType1 = intType2;
        intType2 = swap;
    }

    private static void show(IntType... objs){
        if(objs == null || objs.length == 0 ){
            System.out.println("empty");
            return;
        }

        StringBuilder builder = new StringBuilder();
        int index = 0;
        for(IntType intType : objs){
            builder.append("IntType" + (index + 1) + " --> " + intType);
            builder.append(",");
        }

        String log = builder.deleteCharAt(builder.length() - 1 ).toString();
        System.out.println(log);
    }
}
